package com.yangguang.huahui.entity;

import android.text.TextUtils;

import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.util.HanziToPinyin;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/23.
 */

public class Contact {

    private String msg;
    private int status;
    private List<ListBean> list;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<ListBean> getList() {
        return list;
    }

    public void setList(List<ListBean> list) {
        this.list = list;
    }

    public class ListBean {

        private String id;
        private String for_uid;
        private String to_uid;
        private String createtime;
        private String avatar;
        private String nickname;
        private String initialLetter;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getFor_uid() {
            return for_uid;
        }

        public void setFor_uid(String for_uid) {
            this.for_uid = for_uid;
        }

        public String getTo_uid() {
            return to_uid;
        }

        public void setTo_uid(String to_uid) {
            this.to_uid = to_uid;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getInitialLetter() {
            if (initialLetter == null){
                setUserInitialLetter(this);
            }
            return initialLetter;
        }

        public void setInitialLetter(String initialLetter) {
            this.initialLetter = initialLetter;
        }
        /**
         * set initial letter of according user's nickname( username if no nickname)
         *
         * @param user
         */
        public void setUserInitialLetter(Contact.ListBean user) {
            final String DefaultLetter = "#";
            String letter = DefaultLetter;

            final class GetInitialLetter {
                String getLetter(String name) {
                    if (TextUtils.isEmpty(name)) {
                        return DefaultLetter;
                    }
                    char char0 = name.toLowerCase().charAt(0);
                    if (Character.isDigit(char0)) {
                        return DefaultLetter;
                    }
                    ArrayList<HanziToPinyin.Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1));
                    if (l != null && l.size() > 0 && l.get(0).target.length() > 0)
                    {
                        HanziToPinyin.Token token = l.get(0);
                        String letter = token.target.substring(0, 1).toUpperCase();
                        char c = letter.charAt(0);
                        if (c < 'A' || c > 'Z') {
                            return DefaultLetter;
                        }
                        return letter;
                    }
                    return DefaultLetter;
                }
            }

            if ( !TextUtils.isEmpty(user.getNickname()) ) {
                letter = new GetInitialLetter().getLetter(user.getNickname());
                user.setInitialLetter(letter);
                return;
            }
            if (letter.equals(DefaultLetter) && !TextUtils.isEmpty(user.getNickname())) {
                letter = new GetInitialLetter().getLetter(user.getNickname());
            }
            user.setInitialLetter(letter);
        }

    }

}
